package com.example.demo.security.sysLogin.controller;


import com.example.demo.common.ResultBean;
import com.example.demo.common.ResultBeanCodeEnum;
import com.example.demo.security.sysLogin.service.LoginService;
import com.example.demo.security.sysUser.entity.UserBean;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("login")
public class LoginController {

    @Autowired
    private LoginService loginService;

    private final String file_path="security/login/";

    /**
     * 跳转登录页
     * @author 自嘲
     * @param request
     * @return 跳转登录页的字符串
     */
    @RequestMapping("toLogin")
    public String toLogin(HttpServletRequest request){
        try {
            String message=request.getParameter("message");
            if (!message.isEmpty()){
                request.setAttribute("message",message);
            }
        }catch (Exception e){
            request.setAttribute("message","");
        }
        String url=file_path+"login";
        return url;
    }

    /**
     * 跳转注册用户页面
     * @author 自嘲
     * @return 跳转注册页的字符串
     */
    @RequestMapping("toRegister")
    public String toRegister(){
        String url=file_path+"register";
        return url;
    }

    /**
     * 注册用户
     * @author 自嘲
     * @param userBean
     * @return
     */
    @RequestMapping("register")
    @ResponseBody
    public ResultBean register(UserBean userBean){
        ResultBean resultBean = new ResultBean();
        try {
            Integer i=loginService.registerUser(userBean);
            if (i==-1){
                resultBean.fillCode(ResultBeanCodeEnum.OPER_USER_EXIST);
            }else if(i==0){
                resultBean.fillCode(ResultBeanCodeEnum.OPERA_FAIL);
            }else {
                resultBean.fillCode(ResultBeanCodeEnum.OPERA_SUCCESS);
            }
        }catch (Exception e){
            e.printStackTrace();
            resultBean.fillCode(ResultBeanCodeEnum.OPERA_EXCEPTION);
        }
        return resultBean;
    }

    /**
     * 验证登录用户名密码是否正确
     * @author 自嘲
     * @param attributes
     * @param request
     * @param loginName 用户输入的登录名
     * @param password 用户输入的密码
     * @return 重定向到登录或者主页
     */
    @RequestMapping("login")
    public String login(RedirectAttributes attributes, HttpServletRequest request,
                        @RequestParam("loginName") String loginName, @RequestParam("password") String password){
        try {
            UserBean userBean=loginService.queryByName(loginName);
            if (userBean!=null){
                if (userBean.getUserPwd().equals(password)){
                    request.getSession().setAttribute("id",userBean.getId());
                    request.getSession().setAttribute("name",userBean.getUserName());
                    loginService.updateUserStateOnLine(userBean.getId());
                    return "redirect:/user/toMenu";
                }else {
                    String message="密码错误，请重新输入";
                    attributes.addFlashAttribute("message",message);
                    return "redirect:/login/toLogin";
                }
            }else {
                String message="用户名不存在或用户在线";
                attributes.addFlashAttribute("message",message);
                return "redirect:/login/toLogin";
            }
        }catch (Exception e){
            String message="用户名不存在或用户在线";
            attributes.addFlashAttribute("message",message);
            return "redirect:/login/toLogin";
        }
    }

    /**
     * 退出登录
     * @author 自嘲
     * @param request
     * @return 跳转登录页
     */
    @RequestMapping("outLogin")
    public String outLogin(HttpServletRequest request){
        Integer id=Integer.parseInt(request.getSession().getAttribute("id").toString());
        loginService.updateUserState(id);
        request.getSession().removeAttribute("id");
        request.getSession().removeAttribute("name");
        String url="redirect:/login/toLogin";
        return url;
    }

    /**
     * 非正常退出登录
     * @param request
     */
    @RequestMapping("outLogins")
    @ResponseBody
    public void outLogins(HttpServletRequest request){
        Integer id=Integer.parseInt(request.getSession().getAttribute("id").toString());
        loginService.updateUserState(id);
        request.getSession().removeAttribute("id");
        request.getSession().removeAttribute("name");
    }
}
